/*
 * RReadWriteImplementation.h
 *
 *  Created on: 16.08.2011
 *      Author: kirill
 *      Copyright: LGPL
 */

#ifndef RREADWRITEIMPLEMENTATION_H_
#define RREADWRITEIMPLEMENTATION_H_

#include "BasicTypes.h"
#include "Constants.h"
#include "RReadWriteImplementation.h"
#include "TThreadSafeQueue.h"
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/thread/thread.hpp>
/*
 *
 */
class RReadWriteImplementation
{
private:
protected:
	boost::function<bool( const UINT, UINT64 )> connectionErrorCallback;
	boost::asio::ip::tcp::socket *connectionSocket;
	boost::asio::ip::tcp::endpoint *endpoint;
	TThreadSafeQueue<STANDART_NETWORK_MESSAGE*> *incomeQueue;
	TThreadSafeQueue<STANDART_NETWORK_MESSAGE*> *outputQueue;
	boost::asio::io_service            *io_service;
	boost::thread                      syncWritingThread;


    bool ConnectionEstabilished;
	volatile bool ReadWriteAllow; //Disable/Enable read/write
	UINT		  connectionId;
	STANDART_NETWORK_MESSAGE *IncomeMessage;
	size_t                    DataArrayReadSize;
	BYTE                     *DataArray;
	BYTE                     *WritingCursor;

	void ConnectionHandler(const boost::system::error_code &ErrorCode);
	void ReadHeader( const boost::system::error_code &ec );
	void ReadData( const boost::system::error_code &ec );
	void WritingThread();

	RReadWriteImplementation();
	RReadWriteImplementation( UINT ConnectionId,
							  boost::function<bool( const UINT, UINT64 )> ConnectionStatusCallback,
							  boost::asio::ip::tcp::endpoint *Endpoint,
							  boost::asio::io_service        *Io_service,
							  TThreadSafeQueue<STANDART_NETWORK_MESSAGE*> *IncomeQueue );
	virtual ~RReadWriteImplementation();
public:
};

#endif /* RREADWRITEIMPLEMENTATION_H_ */
